package com.wanmait.gohome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.gohome.mapper.ConfigMapper;
import com.wanmait.gohome.mapper.UserinfoMapper;
import com.wanmait.gohome.pojo.Config;
import com.wanmait.gohome.pojo.Userinfo;
import com.wanmait.gohome.service.UserinfoService;
import com.wanmait.gohome.util.SysContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserinfoServiceImpl implements UserinfoService {
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private ConfigMapper configMapper;
    @Override
    public void insert(Userinfo object) {

    }

    @Override
    public void update(Integer id) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public List<Userinfo> findAll() {
        return null;
    }

    /**
     *
     * @param id 通过ID 查询 信息
     * @return
     */
    @Override
    public Userinfo findById(Integer id) {
        return userinfoMapper.findById(id);
    }



    @Override
    public List<Userinfo> findAll(Userinfo object) {
        return null;
    }

    @Override
    public PageInfo<Userinfo> findPage(Userinfo object) {
        return null;
    }

    @Override
    public PageInfo<Userinfo> findPage1() {
        int pageNum= SysContext.getPageNum();
        int pageSize=0;

        try {
            Config config = configMapper.selectManagePageInteger();
            pageSize = Integer.parseInt(config.getConfigValue());//数据库里取
        } catch (NumberFormatException e) {
            pageSize=10;
        }

        PageHelper.startPage(pageNum,pageSize);//pageSize

        List<Userinfo> userinfos    = userinfoMapper.selectList1();

        PageInfo<Userinfo> pageInfo = new PageInfo<>(userinfos, 7);

        return pageInfo;
    }


    /**
     * @param userId
     * @param taskId  个人中心  放弃任务
     */
    @Override
    public void giveUpByTaskId(Integer userId, Integer taskId) {
        userinfoMapper.updateByTaskIdAndUserId(userId,taskId);
    }

    /**
     *
     * @param userId
     * @param taskId  完成任务 审核
     */
    @Override
    public void finishByTaskId(Integer userId, Integer taskId,String finishReason) {
        userinfoMapper.updateByUAndT(userId,taskId,finishReason);
    }


    @Override
    public PageInfo<Userinfo> findPage() {
        int pageNum= SysContext.getPageNum();
        int pageSize=0;

        try {
            Config config = configMapper.selectManagePageInteger();
            pageSize = Integer.parseInt(config.getConfigValue());//数据库里取
        } catch (NumberFormatException e) {
            pageSize=10;
        }

        PageHelper.startPage(pageNum,pageSize);//pageSize

        List<Userinfo> userinfos    = userinfoMapper.selectList();

        PageInfo<Userinfo> pageInfo = new PageInfo<>(userinfos, 7);

        return pageInfo;
    }



    @Override
    public PageInfo<Userinfo> conditionQuery(String name,Integer province,Integer city,Integer area) {
        int pageNum= SysContext.getPageNum();
        int pageSize=0;

        try {
            Config config = configMapper.selectManagePageInteger();
            pageSize = Integer.parseInt(config.getConfigValue());//数据库里取
        } catch (NumberFormatException e) {
            pageSize=10;
        }

        PageHelper.startPage(pageNum,pageSize);//pageSize

        List<Userinfo> userinfos    = userinfoMapper.findtitle(name,province,city,area);

        PageInfo<Userinfo> pageInfo = new PageInfo<>(userinfos, 7);

        return pageInfo;
    }

    @Override
    public void updateEnable(Userinfo userinfo) {//封停账号更改enable值
       userinfoMapper.updateEnable(userinfo);

    }
    /**
     *
     * @param phone  检查手机号是否重复
     * @return
     */
    @Override
    public String selectByPhone(String phone) {
        return userinfoMapper.selectByPhone(phone);
    }


    /**
     *
     * @param userinfo   用户注册
     */
    @Override
    public void insertUser(Userinfo userinfo) {
        userinfoMapper.insertUser(userinfo);
    }

    /**
     *
     * @param userinfo   用户登录
     */
    @Override
    public Userinfo findByPhoneAndPass(Userinfo userinfo) {
        return userinfoMapper.findByPhoneAndPass(userinfo);
    }

    /**
     *
     * @param userinfo 登录时修改时间  次数  ip
     */
    @Override
    public void updateById(Userinfo userinfo) {
        userinfoMapper.updateById(userinfo);
    }


    /**
     *
     * @param phone  用户自动登录
     * @return
     */
    @Override
    public Userinfo findByPhone(String phone) {
        return userinfoMapper.findByPhone(phone);
    }


    /**
     *
     * @param userinfo   修改密码
     */
    @Override
    public void updateByPhone(Userinfo userinfo) {
        userinfoMapper.updateByPhone(userinfo);
    }

    /**
     *
     * @param id  查询用户信息详情
     * @return
     */
    @Override
    public Userinfo findUserdetails(String id) {


        return userinfoMapper.findUserinf(id);
    }
//后台查询志愿者
    @Override
    public PageInfo<Userinfo> selectVolunteers() {
       int pageNum =SysContext.getPageNum();
       int pageSize = 10;
       PageHelper.startPage(pageNum,pageSize);
       List<Userinfo> volunteers=userinfoMapper.selectVolunteers();
       PageInfo<Userinfo> pageInfo = new PageInfo<>(volunteers,5);
        return pageInfo;
    }

    @Override
    public void deleteVolunteer(Integer id) {
      userinfoMapper.deleteVolunteer(id);
    }

    /**
     * 申请成为志愿者方法
     * @param userinfo
     */
    @Override
    public void updateApplyVolunteer(Userinfo userinfo) {
        userinfoMapper.updateApplyVolunteer(userinfo);
    }

    @Override
    public Userinfo findVolunteerById(Integer id) {
        return userinfoMapper.selectVolunteerById(id);
    }

    /**
     *
     * @param userinfo  个人中心修改头像
     */
    @Override
    public void updatePicById(Userinfo userinfo) {
        userinfoMapper.updatePicById(userinfo);
    }

    /**
     *
     * @param userinfo 个人中心修改信息
     */
    @Override
    public void updateByUserId(Userinfo userinfo) {
        userinfoMapper.updateByUserId(userinfo);
    }

    @Override
    public void recover(Integer id) {
        userinfoMapper.recover(id);
    }

    @Override
    public PageInfo<Userinfo> volunteerQuery(String title) {
            int pageNum= 1;
        int pageSize = 10;
        PageHelper.startPage(pageNum,pageSize);//pageSize

        List<Userinfo> seachVolunteer    = userinfoMapper.searchVolunteer(title);

        PageInfo<Userinfo> pageInfo = new PageInfo<>(seachVolunteer, 7);

        return pageInfo;

    }
//得到志愿者申请个数
    @Override
    public int getVolunteerApplyCount() {
    return userinfoMapper.getVolunteerApplyCount();
    }

    @Override
    public PageInfo<Userinfo> selectVolunteerApply() {
        int pageNum= SysContext.getPageNum();
        int pageSize=10;
        PageHelper.startPage(pageNum,pageSize);//pageSize
        List<Userinfo> volunteerApply = userinfoMapper.selectVolunteerApply();
        PageInfo<Userinfo> pageInfo = new PageInfo<>(volunteerApply,5);
        return pageInfo;
    }

    @Override
    public void VolunteerPass(Integer id) {
        userinfoMapper.VolunteerPass(id);
    }

    @Override
    public void VolunteerReject(Integer id) {
       userinfoMapper.volunteerReject(id);
    }

    @Override
    public void insertReject(Userinfo userinfo) {
      userinfoMapper.insertReject(userinfo);

    }

    @Override
    public Userinfo volunteerApplyInfo(Integer id) {
        return userinfoMapper.volunteerApplyInfo(id);
    }



}


